<!DOCTYPE HTML PUBLIC "-//ORA//DTD CD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>[Chapter 5] Collections</TITLE>
<META NAME="author" CONTENT="Mark Grand and Jonathan Knudsen">
<META NAME="date" CONTENT="Fri Aug  8 16:11:51 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="Java">
<META NAME="title" CONTENT="Java Fundamental Classes Reference">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java Fundamental Classes Reference" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch04_03.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 5</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch05_02.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<H1 CLASS=chapter><A CLASS="TITLE" NAME="JFC-CH-5">5. Collections</A></H1>

<DIV CLASS=htmltoc>

<p>
<b>Contents:</b><br>
Enumerations<br>
<A HREF="ch05_02.htm">Vectors</A><BR>
<A HREF="ch05_03.htm">Stacks</A><BR>
<A HREF="ch05_04.htm">Hashtables</A><BR>

<p>
</DIV>

<P CLASS=para>
Java provides a number of utility classes that help you to manage a
collection of objects. These collection classes allow you to work with
objects without regard to their types, so they can be extremely useful
for managing objects at a high level of abstraction. This chapter
describes the following collection classes:

<P>
<UL CLASS=itemizedlist>
<li CLASS=listitem>The <tt CLASS=literal>java.util.Vector</tt> 
class, which represents a dynamic array of objects. 

<P>
<li CLASS=listitem>The <tt CLASS=literal>java.util.Stack</tt> class, 
which represents a dynamic stack of objects. 

<P>
<li CLASS=listitem>The <tt CLASS=literal>java.util.Dictionary</tt> 
class, which is an <tt CLASS=literal>abstract</tt> 
class that manages a collection of objects by associating a key with each 
object. 

<P>
<li CLASS=listitem>The <tt CLASS=literal>java.util.Hashtable</tt> 
class, which is a subclass of <tt CLASS=literal>java.util.Dictionary</tt> 
that implements a specific algorithm to associate keys with objects. Given 
a key, a <tt CLASS=literal>Hashtable</tt> can retrieve 
the associated object with little or no searching. 

<P>
<li CLASS=listitem>The <tt CLASS=literal>java.util.Enumeration</tt> 
interface, which supports sequential access to a set of elements. 

<P>
</UL>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JFC-CH-5-SECT-1">5.1 Enumerations</A></h2>

<P CLASS=para>
<A NAME="CH05.ENUM"></A>The <tt CLASS=literal>Enumeration</tt> interface is 
implemented by classes that provide serial access to a set of elements, 
or objects, in a collection. An object that implements the <tt CLASS=literal>Enumeration</tt> 
interface provides two methods for dealing with the set: <tt CLASS=literal>nextElement()</tt> 
and <tt CLASS=literal>hasMoreElements()</tt>. The 
<tt CLASS=literal>nextElement()</tt> method returns 
a value of type <tt CLASS=literal>Object</tt>, so 
it can be used with any kind of collection. When you remove an object from 
an <tt CLASS=literal>Enumeration</tt>, you may need 
to cast the object to the appropriate type before using it. You can iterate 
through the elements in an <tt CLASS=literal>Enumeration</tt> 
only once; there is no way to reset it to the beginning or move backwards 
through the elements. 

<P CLASS=para>
Here is an example that prints the contents of an object the implements 
the <tt CLASS=literal>Enumeration</tt> interface: 

<DIV CLASS=screen>
<P>
<PRE>
static void printEnumeration(Enumeration e) {
    while (e.hasMoreElements()) {
        System.out.println(e.nextElement());
    }
</PRE>
</DIV>

<P CLASS=para>
Note that the above method is able to print all of the objects in the <tt CLASS=literal>Enumeration</tt> 
without knowing their class types because the <tt CLASS=literal>println()</tt> 
method handles objects of any type. 

<P CLASS=para>
A number of classes in the Java API provide a method that returns a reference 
to an <tt CLASS=literal>Enumeration</tt> object, rather 
than implementing the <tt CLASS=literal>Enumeration</tt> 
interface directly. For example, as you'll see shortly, the <tt CLASS=literal>Vector</tt> 
class provides an <tt CLASS=literal>elements()</tt> 
method that returns an <tt CLASS=literal>Enumeration</tt> 
of the objects in a <tt CLASS=literal>Vector</tt> 
object. 

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch04_03.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch05_02.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>Generating Exceptions</td>
<td width=171 align=center valign=top><a href="index/idx_0.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>Vectors</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
